from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.mixins import CreateModelMixin
from rest_framework.generics import GenericAPIView,RetrieveAPIView,UpdateAPIView
from django_redis import get_redis_connection
from meiduo_mall.libs.yuntongxun.sms import CCP
from random import randint
from celery_tasks.sms_code.tasks import send_sms_code
from .models import User
from users.serializers import UserSerializer, UserIndexSerializer, UserEmailSerializer
from itsdangerous import TimedJSONWebSignatureSerializer as TJWSS
from django.conf import settings


# Create your views here.

class SMS_code_view(APIView):
    def get(self, request, mobile):
        # 获取手机号
        # 验证手机号

        # 链接数据库
        conn = get_redis_connection('sms_code')
        # 生成管道对象
        lp = conn.pipeline()
        # 判断访问价格
        a = conn.get("key:%s" % mobile)
        if a:
            print(a)
            return Response({"error": "小家伙"}, status=403)
        # 生成验证码
        sms_code = "%06d" % randint(0, 999999)
        print(sms_code)
        # 保存验证码 (指定redis数据库)
        lp.setex(mobile, 300, sms_code)
        # 保存fog记录第一次访问事件，防止恶意访问
        lp.setex("key:%s" % mobile, 60, 2)
        # 通过管道写入数据
        lp.execute()
        # celety发送验证码
        send_sms_code.delay(mobile, sms_code)
        # 返回数据

        return Response("ok")


class User_name(APIView):
    def get(self,request,name):
        # 获取数据库数据
        count = User.objects.filter(username=name).count()
        # 返回数据
        return Response({'count':count})


class User_mobile(APIView):
    def get(self,request,mobile):
        # 获取数据库数据
        count = User.objects.filter(mobile=mobile).count()
        # 返回数据
        return Response({'count':count})

#
# class User_register(GenericAPIView):
#     serializer_class = UserSerializer
#
#     def post(self,request):
#         data = request.data
#         res = self.get_serializer(data=data)
#         res.is_valid()
#         print(res.is_valid())
#         print("up")
#         res.save()
#         print("down")
#         return Response(res.data)

# 用户注册的视图
class User_register(APIView):
    # serializer_class = UserSerializer

    def post(self,request):
        data = request.data
        print(data)
        res = UserSerializer(data=data)
        res.is_valid()
        print(res.errors)
        res.save()
        return Response(res.data)


# 登陆成功之后，点击用户中心跳转的视图
class User_index(RetrieveAPIView):
    serializer_class = UserIndexSerializer
    queryset = User.objects.all()

    def get_object(self):
        return self.request.user


# 设置邮箱
class User_email(UpdateAPIView):
    serializer_class = UserEmailSerializer
    def get_object(self):
        return self.request.user


# 返回邮箱验证页面
class Email_active(APIView):
    def get(self,request):
        # 获取token
        token = request.query_params.get('token')
        # 解密token
        tjwss = TJWSS(settings.SECRET_KEY, 300)
        token = tjwss.loads(token)
        # 获取用户对象
        user = User.objects.get(username=token)
        print(user)
        # 变更该用户email_active
        user.email_active = True
        user.save()
        # 返回信息
        return Response({"email_active":user.email_active})



